home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / master / Examples / Visual / VOpts / vopts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-01  |  6.7 KB  |  302 lines

  1. #include "vopts.h"
  2. #include "vopts_rev.h"
  3.  
  4. Prototype int CreateWindow(void);
  5. Prototype void DestroyWindow(void);
  6. Prototype char *SkipAss(const char *);
  7.  
  8. void myexit(void);
  9. int main(int, char **);
  10.  
  11. struct GLOBAL global;
  12.  
  13. struct Library *GadToolsBase;
  14. struct Library *AslBase;
  15. struct Library *ArpBase;
  16.  
  17. BPTR XSaveLock;
  18. short XSaveLockValid;
  19.  
  20. char    *InitialFileName;
  21. short   FileSpecified;
  22. char    Buf[256];
  23. short   didgadgets;
  24.  
  25. const char version[] = VERSTAG;
  26.  
  27. /*
  28.  *  WORKBENCH STARTUP
  29.  *
  30.  *  Parse tooltypes as a command.  Note that we are CD'd into the
  31.  *  application's directory, not the tooltype's icon's dir.
  32.  */
  33.  
  34. void
  35. myexit()
  36. {
  37.    if (XSaveLockValid) {
  38.       CurrentDir(XSaveLock);
  39.       XSaveLockValid = 0;
  40.    }
  41. }
  42.  
  43. int
  44. wbmain(wbs)
  45. struct WBStartup *wbs;
  46. {
  47.    DiskObject *dob;
  48.    short i;
  49.    short j;
  50.    short abortIt = 0;
  51.  
  52.    /*
  53.     *  Search for options, set current directory to last valid
  54.     *  disk object.  Run commands specified by tool types in order
  55.     */
  56.  
  57.    for (i = 0; i < wbs->sm_NumArgs; ++i)
  58.    {
  59.       BPTR saveLock = CurrentDir((BPTR)wbs->sm_ArgList[i].wa_Lock);
  60.  
  61.       if (i == wbs->sm_NumArgs - 1 && FileSpecified == 0)
  62.       {
  63.          InitialFileName = strdup(wbs->sm_ArgList[i].wa_Name);
  64.       }
  65.  
  66.       if (dob = GetDiskObject(wbs->sm_ArgList[i].wa_Name))
  67.       {
  68.          for (j = 0; dob->do_ToolTypes[j]; ++j)
  69.          {
  70.             char *ptr = dob->do_ToolTypes[j];
  71.  
  72.             if (strnicmp(ptr, "FILE=", 5) == 0)
  73.             {
  74.                InitialFileName = strdup(SkipAss(ptr));
  75.                FileSpecified = 1;
  76.             }
  77.             else
  78.             {
  79.                /* OK to use any text slot, we're about to bail out */
  80.                strcpy(global.text[1], "Bad ToolType:");
  81.                if (request(0, 1, ptr, NULL))
  82.                {
  83.                   abortIt = 1;
  84.                   break;
  85.                }
  86.             }
  87.          }
  88.          FreeDiskObject(dob);
  89.       }
  90.       CurrentDir(saveLock);
  91.       if (abortIt)
  92.           break;
  93.    }
  94.    XSaveLock = CurrentDir((BPTR)wbs->sm_ArgList[wbs->sm_NumArgs-1].wa_Lock);
  95.    XSaveLockValid = 1;
  96.    atexit(myexit);
  97.  
  98.    if (abortIt == 0)
  99.       main(1, NULL);
  100. }
  101.  
  102. /*
  103.  *  CLI STARTUP
  104.  *
  105.  */
  106.  
  107. int
  108. main(argc, argv)
  109. int argc;
  110. char **argv;
  111. {
  112.    struct IntuiMessage *msg;
  113.    int parse_fail;
  114.  
  115.    /*
  116.     *   Open GadToolsBase/AslBase manually so we don't exit if they
  117.     *   don't exist.
  118.     */
  119.  
  120.     if (argc != 1) {
  121.         InitialFileName = argv[1];
  122.         FileSpecified = 1;
  123.     }
  124.  
  125.    GadToolsBase = OpenLibrary("gadtools.library", 0);
  126.    AslBase      = OpenLibrary("asl.library", 0);
  127.  
  128.    memset(&global, 0, sizeof(global));
  129.  
  130.    FillIn_RenderInfo(&global.ri, NULL);
  131.  
  132.    /* Provide a default height/width so that we will get any errors displayed well */
  133. /*
  134.    global.width = 320;
  135. */
  136.    global.width = (3 * global.ri.ScreenWidth) / 4;
  137.    global.height = global.ri.WindowTitle;
  138.  
  139.    global.ri.WindowTitle  += MARGIN_TOP;
  140.    global.ri.WindowLeft   += MARGIN_LEFT;
  141.    global.ri.WindowRight  += MARGIN_RIGHT;
  142.    global.ri.WindowBottom += MARGIN_BOTTOM;
  143.  
  144.    if (AslBase != NULL)
  145.       global.freq = AllocFileRequest();
  146.    else
  147.    {
  148.       ArpBase = OpenLibrary("arp.library", 0);
  149.       if (ArpBase != NULL)
  150.          global.freq = ArpAllocFreq();
  151.    }
  152.  
  153.    parse_fail = parse_config(ALT_CONFIG_FILE);
  154.    if (parse_fail == 2) /* failed to open file */
  155.       parse_fail = parse_config(CONFIG_FILE);
  156.    if ((!parse_fail) &&
  157.        (!init_gad_sizes()))
  158.    {
  159.       init_gadgets();
  160.       didgadgets = 1;
  161.    }
  162.    else
  163.    {
  164.       didgadgets = 0;
  165.    }
  166.  
  167.    if (!CreateWindow())
  168.    {
  169.       if (didgadgets)
  170.       {
  171.          set_gadgets(1);
  172.          do_command("READ ENV");
  173.  
  174.          if (FileSpecified) {
  175.             sprintf(Buf, "READ %s", InitialFileName);
  176.             do_command(Buf);
  177.          }
  178.       }
  179.  
  180.       while(!global.done)
  181.       {
  182.          WaitPort(global.window->UserPort);
  183.          while(msg = (struct IntuiMessage *)GetMsg(global.window->UserPort))
  184.          {
  185.             ULONG class;
  186.             APTR iaddr;
  187.             USHORT code;
  188.             USHORT qual;
  189.             struct XItem *xitem;
  190.  
  191.             code = msg->Code;
  192.             class = msg->Class;
  193.             iaddr = msg->IAddress;
  194.             qual  = msg->Qualifier;
  195.  
  196.             ReplyMsg((struct Message *)msg);
  197.  
  198.             switch(class)
  199.             {
  200.                case CLOSEWINDOW:
  201.                   global.done = 1;
  202.                   break;
  203.                case GADGETDOWN:
  204.                case GADGETUP:
  205.                   handle_hit((struct Gadget *)iaddr,
  206.                       (qual & (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)) ? 1 : 0);
  207.                   break;
  208.                case MENUPICK:
  209.                   while (code != MENUNULL)
  210.                   {
  211.                      xitem = (struct XItem *)
  212.                              ItemAddress(global.menu, code);
  213.                      do_command(xitem->userdata);
  214.                      code = xitem->item.NextSelect;
  215.                   }
  216.                   break;
  217.                case VANILLAKEY:
  218. #ifdef DBG_JGM
  219.                   printf("Key:%d '%c'\n", code, code);
  220. #endif
  221.                   break;
  222.             }
  223.          }
  224.       }
  225.       DestroyWindow();
  226.    }
  227.  
  228.    CleanUp_RenderInfo(&global.ri);
  229.    if (global.freq && (AslBase != NULL)) FreeFileRequest(global.freq);
  230.  
  231.    if (GadToolsBase  != NULL) CloseLibrary(GadToolsBase);
  232.    if (AslBase  != NULL)      CloseLibrary(AslBase);
  233.    if (ArpBase  != NULL)      CloseLibrary(ArpBase);
  234.    return(0);
  235. }
  236.  
  237. /*
  238.  * Create a new window on the screen
  239.  */
  240. int CreateWindow()
  241. {
  242.    struct NewWindow nw;
  243.  
  244.    memset(&nw, 0, sizeof(nw));
  245.  
  246.    nw.Width      = global.width;
  247.    nw.Height     = global.height;
  248.    nw.LeftEdge   = (global.ri.ScreenWidth  - nw.Width ) / 2;
  249.    nw.TopEdge    = (global.ri.ScreenHeight - nw.Height) / 2;
  250.  
  251.    if ((nw.Width < 0) || (nw.Height < 0))
  252.       return(1);
  253.    nw.DetailPen  = nw.BlockPen = -1;
  254.    nw.IDCMPFlags = GADGETUP    |
  255.                    GADGETDOWN  |
  256.                    MENUPICK    |
  257.                    VANILLAKEY  |
  258.                    CLOSEWINDOW;
  259.    nw.Flags      = WINDOWDEPTH    |
  260.                    WINDOWCLOSE    |
  261.                    WINDOWDRAG     |
  262.                    SIMPLE_REFRESH |
  263.                    ACTIVATE       |
  264.                    NOCAREREFRESH;
  265.    nw.Title      = global.title;
  266.    nw.Type       = WBENCHSCREEN;
  267.  
  268.    global.window = OpenWindow(&nw);
  269.    if (global.window == NULL) return(2);
  270.    global.rp = global.window->RPort;
  271.    SetWindowTitles(global.window, global.title, global.wtitle);
  272.    enable_menu();
  273.    return(0);
  274. }
  275.  
  276. /*
  277.  * Make the current window go away
  278.  */
  279. void DestroyWindow()
  280. {
  281.    free_menus(global.menu);
  282.    CloseWindow(global.window);
  283. }
  284.  
  285. /*
  286.  *  Skip whitespace in tooltype assignment
  287.  */
  288.  
  289. char *
  290. SkipAss(ptr)
  291. const char *ptr;
  292. {
  293.    while (*ptr && *ptr != '=')
  294.       ptr++;
  295.    if (*ptr == '=')
  296.    {
  297.       for (ptr++; *ptr == ' ' || *ptr == '\t'; ptr++)
  298.          ;
  299.    }
  300.    return(ptr);
  301. }
  302.